<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="缴费项目" prop="actName">
        <el-input
          v-model="queryParams.actName"
          placeholder="请输入缴费项目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['campus:act_stu:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="act_stuList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="业务流水" align="center" prop="payId"/>
      <el-table-column label="缴费项目" align="center" key="actName" prop="activity.actName"/>
      <el-table-column label="收款方" align="center" key="payee" prop="activity.payee"/>
      <el-table-column label="缴费金额" align="center" key="totalPrice" prop="activity.totalPrice"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['campus:act_stu:edit']"
          >查看
          </el-button>
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-shopping-cart-1"-->
          <!--            @click="createOrder(scope.row)"-->
          <!--            v-hasPermi="['campus:act_stu:remove']"-->
          <!--          >缴费-->
          <!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改缴费名单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <!--      <el-form ref="form" :model="form" :rules="rules" label-width="80px">-->
      <!--        <el-form-item label="缴费项目" prop="actId">-->
      <!--          <el-input v-model="form.actId" placeholder="请输入缴费项目"/>-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="学生" prop="studentId">-->
      <!--          <el-input v-model="form.studentId" placeholder="请输入学生"/>-->
      <!--        </el-form-item>-->
      <!--      </el-form>-->
      <el-row>
        <el-descriptions title="缴费明细" :column="1" size="medium">
          <el-descriptions-item label="缴费项目">{{ activity.actName }}</el-descriptions-item>
          <el-descriptions-item label="缴费金额">{{ activity.totalPrice + ' 元' }}</el-descriptions-item>
          <el-descriptions-item label="缴费说明">{{ activity.remark }}</el-descriptions-item>
        </el-descriptions>
      </el-row>
      <el-row v-if="activity.remarkImg">
        <image-preview :src="activity.remarkImg" :width="50" :height="50"/>
      </el-row>
      <el-row>
        <div v-if="order!==null&&order.status==='SUCCESS'">
          <el-descriptions title="缴费结果" :column="1" size="medium">
            <el-descriptions-item label="缴费状态">支付成功</el-descriptions-item>
            <el-descriptions-item label="缴费时间">2023-02-2308:51:05</el-descriptions-item>
          </el-descriptions>
        </div>
        <div v-if="order==null||order.status==='NOTPAY'">
          <el-card :body-style="{ padding: '0px' }">
            <el-row>
              <span v-if="qrcode!==''">使用微信扫码</span>
            </el-row>
            <el-row>
              <img :src="qrcode" class="image">
            </el-row>
            <div style="padding: 14px;">
              <div class="bottom clearfix">
                <el-button type="primary" @click="createOrder(form)">微信支付</el-button>
                <el-button type="primary" @click="submitForm">支付完成</el-button>
              </div>
            </div>
          </el-card>
        </div>
      </el-row>

      <!--      <div slot="footer" class="dialog-footer">-->
      <!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
      <!--                <el-button @click="cancel">取 消</el-button>-->
      <!--      </div>-->
    </el-dialog>
  </div>
</template>

<script>
import {listAct_stu, getAct_stu} from "@/api/campus/act_stu";
import {addOrders} from "@/api/order/orders";

export default {
  name: "Act_stu",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 缴费名单表格数据
      act_stuList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        actId: null,
        actName: null,
        studentId: null,
        studentName: null,
      },
      // 表单参数
      form: {},
      activity: {},
      student: {},
      order: {},
      // 表单校验
      rules: {},
      qrcode: ""
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询缴费名单列表 */
    getList() {
      this.loading = true;
      listAct_stu(this.queryParams).then(response => {
        this.act_stuList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        payId: null,
        actId: null,
        studentId: null
      };
      this.activity = {};
      this.student = {};
      this.order = {}
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.payId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      const payId = row.payId || this.ids
      getAct_stu(payId).then(response => {
        this.form = response.data
        this.activity = response.data.activity;
        this.student = response.data.students;
        this.order = response.data.orders
        this.open = true;
        this.title = "交费及查询";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.open = false;
      // this.$refs["form"].validate(valid => {
      //   if (valid) {
      //     if (this.form.payId != null) {
      //       updateAct_stu(this.form).then(response => {
      //         this.$modal.msgSuccess("修改成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     } else {
      //       addAct_stu(this.form).then(response => {
      //         this.$modal.msgSuccess("新增成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     }
      //   }
      // });
    },
    createOrder(row) {
      const form = {
        totalPrice: row.activity.totalPrice,
        orderType: "60201",
        orderName: row.activity.actName,
        orderDescription: row.activity.remark,
        orderDetail: JSON.toString([row.activity]),
        outBusinessId: row.payId,
      }
      addOrders(form).then(response => {
        this.$modal.msgSuccess("下单");
        this.qrcode = response.data.qrcode;
      });

    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('campus/act_stu/export', {
        ...this.queryParams
      }, `act_stu_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
